<?php
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act'] == 'read'){
    set_time_limit(0);
	require_once('includes/cls_readexcel.php');
	require(dirname(__FILE__) . '/includes/cls_sms.php');
	$sms = new sms(); 
	$data= new Spreadsheet_Excel_Reader(); 
	$data->setOutputEncoding('utf-8'); 
	if ($_FILES["file"]["error"] > 0){
		$error = urlencode('文件出错了，错误码：'.$_FILES["file"]["error"]);
		if($_FILES["file"]["error"]==4){
			$error = urlencode('请选择excel文件');
		}
		header("Location: notice.php?error=$error\n");exit;
	}else if(end(explode(".",strtolower($_FILES["file"]['name'])))!='xls'){
		$error = urlencode('请选择excel文件');
		header("Location: notice.php?error=$error\n");exit;
	}
	else{
		$datapath = $_FILES["file"]["tmp_name"];
	}
	$data->read($datapath);
	for($i= 2; $i<= $data->sheets[0]['numRows']; $i++){
		$phone = $data->sheets[0]['cells'][$i][1];
		$sn = $data->sheets[0]['cells'][$i][2];
		$express_com = $data->sheets[0]['cells'][$i][3];
		$qudao = $data->sheets[0]['cells'][$i][4];
		$crack_wine = $data->sheets[0]['cells'][$i][5];
		if($crack_wine=='0'){
			$msg = '尊敬的顾客你好，给您漏发的产品已用'.$express_com.'发出，单号为：'.$sn.'给您造成不便敬请谅解。谢谢【'.$qudao.'悦国红酒】';
		}else if($crack_wine=='1'){
			$msg = '尊敬的顾客你好，您的产品已用'.$express_com.'重新发出，单号为：'.$sn.'由于碎酒，给您造成不便敬请谅解。【'.$qudao.'悦国红酒】';
		}else{
			$msg = '';
		}
		if($msg){
			$sms->send($phone,$msg);
		}
	}
}else if($_REQUEST['act'] == 'download'){
	$ok = download(iconv('UTF-8','GB2312','./userUpload/templet/漏发碎酒模板.xls'),iconv('UTF-8','GB2312','漏发碎酒模板.xls'));
	if(!$ok){
		$error = "未找到源文件";
		header("Location: notice.php?error=$error\n");exit;
	}
}


function download($url, $filename) {   
        // 获得文件大小, 防止超过2G的文件, 用sprintf来读
		if(file_exists($url)){
        	$filesize = sprintf ( "%u", filesize ( $url ) ); 
		}  
        if (! $filesize) {   
            return false;   
         }   
         header ( "Content-type:application/octet-stream\n" ); //application/octet-stream   
         header ( "Content-type:unknown/unknown;" );   
         header ( "Content-disposition: attachment; filename=\"" . $filename . "\"" );   
         header ( 'Content-transfer-encoding: binary' );   
        if ($range = getenv ( 'HTTP_RANGE' )) { // 当有偏移量的时候，采用206的断点续传头   
            $range = explode ( '=', $range );   
            $range = $range [1];   
               
             header ( "HTTP/1.1 206 Partial Content" );   
             header ( "Date: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );   
             header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s", filemtime ( $url ) ) . " GMT" );   
             header ( "Accept-Ranges: bytes" );   
             header ( "Content-Length:" . ($filesize - $range) );   
             header ( "Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize );   
             header ( "Connection: close" . "\n\n" );   
         } else {   
             header ( "Content-Length:" . $filesize . "\n\n" );   
            $range = 0;   
         }   
         loadFile ( $url ); 
		 return true;  
    }   
      
    function loadFile($filename, $retbytes = true) {   
        $buffer = '';   
        $cnt = 0;   
        $handle = fopen ( $filename, 'rb' );   
        if ($handle === false) {   
            return false;   
         }   
        while ( ! feof ( $handle ) ) {   
            $buffer = fread ( $handle, 1024 * 1024 );   
            echo $buffer;   
             ob_flush ();   
            flush ();   
            if ($retbytes) {   
                $cnt += strlen ( $buffer );   
             }   
         }   
        $status = fclose ( $handle );   
        if ($retbytes && $status) {   
            return $cnt; // return num. bytes delivered like readfile() does.   
         }   
        return $status;   
    }   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 短信发送 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
</head>
<body>
<h1>
<span class="action-span"><a href="notice.php?act=download" title="模板下载">导入模板下载</a></span>
<span class="action-span1"><a href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 短信发送 </span>
<div style="clear:both"></div>
</h1> 
<?php if(!empty($_GET['error'])){?>
<div class="form-div" id="error">
  <span><a href="#" onclick="javascript:err_hide()" title="关闭"><img src="images/no.gif" border="0" alt="关闭"></a><?php echo urldecode($_GET['error']); ?></span>
</div>
<?php }?>
<div class="form-div">
  <form method="post" action="?act=read" enctype="multipart/form-data">
   Excel<input type="file" name="file" size="50" />
   <input type="submit" name="readexcel" value="短信发送" class="button"/>
   <?php if($dao_str){?><span style="color:red;"><?php echo $dao_str;?></span><?php }?>
  </form>
</div>
  <div>
  	漏发短信内容示例： 尊敬的顾客你好，给您漏发的产品已用韵达发出，单号为：111111111给您造成不便敬请谅解。谢谢【一号店悦国红酒】<br/>
  	碎酒补发内容示例： 尊敬的顾客你好，您的产品已用韵达重新发出，单号为：111111111由于碎酒，给您造成不便敬请谅解。【一号店悦国红酒】
  </div>
  <script type="text/javascript">
var num = <?php echo count($order_list);?>;
for (i=1;i<=num;i++){
    hide_show(i);
}
function hide_show(i){
	//alert(22);
	$("#order_"+i).hover(function() {         
        $("#address_"+i).show();  
    },function(){
           $("#address_"+i).fadeOut(1000);  
        }

   ); 
}

function err_hide(){
	 $("#error").css("display","none"); 
}
</script>
  </body>
</html>